package org.gcu.filestorage.uitls;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class MD5Util {

    private static final char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
    public static String encryptByMD5(String content) {
        log.info("encryptByMD5(), content=" + content);
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(content.getBytes());
            byte[] encrypted = md5.digest();
            char[] result = new char[encrypted.length * 2]; // 将一个字节转换成两位十六进制
            for (int index = 0; index < encrypted.length; index++) {
                result[index] = hexDigits[(encrypted[index] & 0xf0) >>> 4];
                result[index + 1] = hexDigits[(encrypted[index] & 0x0f)];
            }
            return new String(result);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}
